package com.yueka.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yueka.mapper.RoleMapper;
import com.yueka.pojo.PageResult;
import com.yueka.pojo.entity.role.Role;
import com.yueka.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult<Role> list(String keyword, Integer page, Integer pageSize) {
        Page<Role> pageParam = new Page<>(page, pageSize);
        Page<Role> pageList = roleMapper.selectPage(pageParam, Wrappers.lambdaQuery(Role.class)
                .like(Role::getRoleName, keyword));
        return new PageResult<>(pageList.getTotal(), pageList.getRecords());
    }

    @Override
    public Integer delete(Integer[] ids) {
        return roleMapper.delete(ids);
    }

    @Override
    public Integer update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        if (role.getId() == null) {
            role.setCreateTime(LocalDateTime.now());
            return roleMapper.insert(role);
        }
        return roleMapper.update(role);
    }

    @Override
    public List<Role> getAll() {
        return roleMapper.getAll();
    }


}
